#!/usr/bin/perl -w
use strict;
use lib 'lib';
use lib 'extlib';
use MT;
use MT::Template;
use Getopt::Long;

my ($TEMPLATE_NAME, $BLOG_ID, $USAGE);
my $MT_PATH = "";

GetOptions("name=s" => \$TEMPLATE_NAME, "blog=i" => \$BLOG_ID );

$USAGE = <<USAGE
Usage:
mt-rebuild-template --name "<template name>" --blog <blog id number>
USAGE
;

sub rebuild_template
{
	my $mt = $_[0];
	my $template_name = $_[1];
	my $blog_id = $_[2];

	my $template = MT::Template->load({name => $template_name, blog_id => $blog_id })
		or die "Rebuild Error: Can't load template $template_name in blog_id of $blog_id";
	$mt->rebuild_indexes(BlogID => $blog_id, Template => $template, Force => 1)
		or die "Rebuild error: ", $mt->errstr;
}

sub main
{
	my $mt_config = $MT_PATH . 'mt.cfg';
	my $mt = MT->new( Config => $mt_config, Directory => $MT_PATH);
	
	if(defined($TEMPLATE_NAME) && defined($BLOG_ID))
	{
		&rebuild_template($mt, $TEMPLATE_NAME, $BLOG_ID);
	}
	else
	{
		print $USAGE;
	}
}

&main();
